﻿using LJ.Abstractions.Extensions;
using LJ.AspNetCore.Abstractions.Pack;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJ.WebApi.Mvc.Extensions;

public static class AspNetCorePackProviderExtensions
{
    public static IAspNetCorePackProvider UseOpenApi(this IAspNetCorePackProvider packProvider)
    {
        var webApiConfig = packProvider.ServiceProvider.GetConfig<WebApiConfig>();
        packProvider.ApplicationBuilder.UseSwagger();
        packProvider.ApplicationBuilder.UseSwaggerUI(options =>
        {
            options.SwaggerEndpoint($"/swagger/ALL/swagger.json", "ALL");

            var apiDescriptionGroupCollectionProvider = packProvider.ServiceProvider.GetRequiredService<IApiDescriptionGroupCollectionProvider>();
            foreach (var description in apiDescriptionGroupCollectionProvider.ApiDescriptionGroups.Items)
            {
                options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName);
            }


            //设置默认的接口文档展开方式，可选值包括None、List和Full。
            //默认值为None，表示不展开接口文档；
            //List表示只展开接口列表；
            //Full表示展开所有接口详情
            options.DocExpansion(DocExpansion.None); // 设置为完整模式 

            //启用永久授权
            options.EnablePersistAuthorization();
        });
        return packProvider;
    }

   
}
